import {utilities as csUtils} from "@cornerstonejs/core";

function getCTValue(volume, worldPos) {
    const { dimensions, scalarData, imageData } = volume;

    const index = imageData.worldToIndex(worldPos);

    index[0] = Math.floor(index[0]);
    index[1] = Math.floor(index[1]);
    index[2] = Math.floor(index[2]);

    if (!csUtils.indexWithinDimensions(index, dimensions)) {
        return;
    }

    const yMultiple = dimensions[0];
    const zMultiple = dimensions[0] * dimensions[1];

    const value =
        scalarData[index[2] * zMultiple + index[1] * yMultiple + index[0]];

    return value;
}

export {
    getCTValue
}